package com.demo.controller;

import com.demo.entity.Role;
import com.demo.response.Result;
import com.demo.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @ApiOperation("新增角色")
    @PostMapping("/addRole")
    public Result addRole(@Validated @RequestBody Role role, BindingResult result) throws Exception {
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return roleService.addRole(role);
    }

    @ApiOperation("编辑角色")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role, BindingResult result){
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return roleService.updateRole(role);
    }

    @ApiOperation("删除角色")
    @GetMapping("/deleteRole")
    public Result deleteRole(@RequestParam(name = "roleId")Integer roleId,
                             @RequestParam(name = "deleteUserId")Integer deleteUserId) throws Exception {
        return roleService.deleteRole(roleId, deleteUserId);
    }

    @ApiOperation("角色列表")
    @GetMapping("/roleList")
    public Result roleList(@RequestParam(name = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                           @RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                           @RequestParam(name = "roleId",required = false)Integer roleId){
        return roleService.roleList(pageNumber, pageSize, roleId);
    }






}
